﻿'Copyright (c) 2009 Grailtek

'Permission is hereby granted, free of charge, to any person
'obtaining a copy of this software and associated documentation
'files (the "Software"), to deal in the Software without
'restriction, including without limitation the rights to use,
'copy, modify, merge, publish, distribute, sublicense, and/or sell
'copies of the Software, and to permit persons to whom the
'Software is furnished to do so, subject to the following
'conditions:

'The above copyright notice and this permission notice shall be
'included in all copies or substantial portions of the Software.

'THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
'EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
'OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
'NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
'HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
'WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
'FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
'OTHER DEALINGS IN THE SOFTWARE.

Imports System.Runtime.CompilerServices
Imports System.Reflection

Friend Module Utils

    <Extension()> _
    Public Function HasPublicParameterlessConstructor(ByVal type As Type) As Boolean
        If type.IsGenericType Then Return False
        If type.IsValueType Then Return True
        For Each ctorInfo As ConstructorInfo In type.GetConstructors(BindingFlags.Public Or BindingFlags.Instance)
            If ctorInfo.GetParameters.GetUpperBound(0) = -1 Then Return True
        Next
    End Function

    Public ReadOnly LocalAssemblyName As New AssemblyName("ExecutorServicesTypeGenerator")
    Public ReadOnly objCtor As ConstructorInfo = GetType(Object).GetConstructor(Type.EmptyTypes)

    Public Function GetMethods(ByVal type As Type) As List(Of MethodInfo)
        Dim ret As New List(Of MethodInfo)
        If Not type.IsInterface Then
            ret.AddRange(type.GetMethods(BindingFlags.FlattenHierarchy))
        Else
            ret.AddRange(GetInterfaceMethods(type))
        End If
        Return ret
    End Function

    Public Function GetInterfaceMethods(ByVal type As Type) As MethodInfo()
        Dim ret As New List(Of MethodInfo)
        ret.AddRange(type.GetMethods)
        For Each t As Type In type.GetInterfaces()
            GetInterfaceMethodsRecursive(ret, t)
        Next
        Return ret.Distinct.ToArray
    End Function

    Public Sub GetInterfaceMethodsRecursive(ByVal list As List(Of MethodInfo), ByVal type As Type)
        list.AddRange(type.GetMethods)
        For Each t As Type In type.GetInterfaces()
            GetInterfaceMethodsRecursive(list, t)
        Next
    End Sub

    Public Function GetProperties(ByVal type As Type) As PropertyInfo()
        Dim ret(-1) As PropertyInfo
        If Not type.IsInterface Then
            ret = type.GetProperties(BindingFlags.SetProperty Or BindingFlags.Instance Or BindingFlags.Public)
        Else
            ret = GetInterfaceProperties(type)
        End If
        Return ret.ToArray
    End Function

    Public Function GetInterfaceProperties(ByVal type As Type) As PropertyInfo()
        Dim ret As New List(Of PropertyInfo)
        GetInterfacePropertiesRecursive(ret, type)
        Return ret.Distinct.ToArray
    End Function

    Public Sub GetInterfacePropertiesRecursive(ByVal list As List(Of PropertyInfo), ByVal type As Type)
        list.AddRange(type.GetProperties)
        For Each t As Type In type.GetInterfaces()
            If Not t Is GetType(Void) Then GetInterfacePropertiesRecursive(list, t)
        Next
    End Sub

End Module
